From 904eecd1edbd20f3459a08ab6203a642a730a914 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Mon, 10 Feb 2020 12:33:17 +0100 Subject: [PATCH] icon paintable: Replace get_filename and get_resource_path with get_file() This returns a GFile which can represent both the above. --- demos/icon-browser/iconbrowserwin.c | 5 +-- docs/reference/gtk/gtk4-sections.txt | 3 +- gtk/gtkicontheme.c | 48 ++++++++++++++-------------- gtk/gtkicontheme.h | 4 +-- tests/testicontheme.c | 15 ++++----- testsuite/gtk/icontheme.c | 17 ++++++++-- 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/demos/icon-browser/iconbrowserwin.c b/demos/icon-browser/iconbrowserwin.c index d8f684a7dd..c0fe825031 100644 --- a/demos/icon-browser/iconbrowserwin.c +++ b/demos/icon-browser/iconbrowserwin.c @@ -423,7 +423,6 @@ get_file (GValue *value, GtkIconTheme *icon_theme; const char *name; GtkIconPaintable *info; - GFile *file; name = gtk_image_get_icon_name (GTK_IMAGE (data)); icon_theme = gtk_icon_theme_get_for_display (gtk_widget_get_display (GTK_WIDGET (data))); @@ -434,9 +433,7 @@ get_file (GValue *value, 32, 1, gtk_widget_get_direction (GTK_WIDGET (data)), 0); - file = g_file_new_for_path (gtk_icon_paintable_get_filename (info)); - g_value_set_object (value, file); - g_object_unref (file); + g_value_take_object (value, gtk_icon_paintable_get_file (info)); g_object_unref (info); } diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index aa7726bb53..3802214025 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -5009,8 +5009,7 @@ gtk_icon_theme_choose_icon_finish gtk_icon_theme_lookup_by_gicon gtk_icon_theme_list_icons gtk_icon_theme_get_icon_sizes -gtk_icon_paintable_get_filename -gtk_icon_paintable_get_resource_path +gtk_icon_paintable_get_file gtk_icon_paintable_get_icon_name gtk_icon_paintable_is_symbolic diff --git a/gtk/gtkicontheme.c b/gtk/gtkicontheme.c index 0232396f19..02ac30a197 100644 --- a/gtk/gtkicontheme.c +++ b/gtk/gtkicontheme.c @@ -3262,41 +3262,41 @@ gtk_icon_paintable_class_init (GtkIconPaintableClass *klass) gobject_class->finalize = gtk_icon_paintable_finalize; } -/** - * gtk_icon_paintable_get_filename: - * @self: a #GtkIcon - * - * Gets the filename for the icon. - * - * Returns: (nullable) (type filename): the filename for the icon, or %NULL - * if the icon is not represented by a filename. - */ -const gchar * -gtk_icon_paintable_get_filename (GtkIconPaintable *icon) +static GFile * +new_resource_file (const char *filename) { - g_return_val_if_fail (icon != NULL, NULL); + char *escaped = g_uri_escape_string (filename, + G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE); + char *uri = g_strconcat ("resource://", escaped, NULL); + GFile *file = g_file_new_for_uri (uri); - if (!icon->is_resource) - return icon->filename; - return NULL; + g_free (escaped); + g_free (uri); + + return file; } /** - * gtk_icon_paintable_get_resource_path: + * gtk_icon_paintable_get_file: * @self: a #GtkIcon * - * Gets the resource path for the icon. + * Gets the #GFile that was used to load the icon, or %NULL if the icon was + * not loaded from a file. * - * Returns: (nullable) (type filename): the resource for the icon, or %NULL - * if the icon is not represented by a resource. + * Returns: (nullable) (transfer full): the #GFile for the icon, or %NULL. + * Free with g_object_unref(). */ -const gchar * -gtk_icon_paintable_get_resource_path (GtkIconPaintable *icon) +GFile * +gtk_icon_paintable_get_file (GtkIconPaintable *icon) { - g_return_val_if_fail (icon != NULL, NULL); + if (icon->filename) + { + if (icon->is_resource) + return new_resource_file (icon->filename); + else + return g_file_new_for_path (icon->filename); + } - if (icon->is_resource) - return icon->filename; return NULL; } diff --git a/gtk/gtkicontheme.h b/gtk/gtkicontheme.h index f2904708f1..cf73c284cd 100644 --- a/gtk/gtkicontheme.h +++ b/gtk/gtkicontheme.h @@ -140,9 +140,7 @@ GDK_AVAILABLE_IN_ALL GType gtk_icon_paintable_get_type (void) G_GNUC_CONST; GDK_AVAILABLE_IN_ALL -const gchar * gtk_icon_paintable_get_filename (GtkIconPaintable *self); -GDK_AVAILABLE_IN_ALL -const gchar * gtk_icon_paintable_get_resource_path (GtkIconPaintable *self); +GFile * gtk_icon_paintable_get_file (GtkIconPaintable *self); GDK_AVAILABLE_IN_ALL const gchar * gtk_icon_paintable_get_icon_name (GtkIconPaintable *self); GDK_AVAILABLE_IN_ALL diff --git a/tests/testicontheme.c b/tests/testicontheme.c index d4b330d664..d161dd6566 100644 --- a/tests/testicontheme.c +++ b/tests/testicontheme.c @@ -114,6 +114,8 @@ main (int argc, char *argv[]) } else if (strcmp (argv[1], "lookup") == 0) { + GFile *file; + if (argc < 4) { g_object_unref (icon_theme); @@ -128,17 +130,12 @@ main (int argc, char *argv[]) scale = atoi (argv[5]); icon = gtk_icon_theme_lookup_icon (icon_theme, argv[3], NULL, size, scale, direction, flags); + file = gtk_icon_paintable_get_file (icon); g_print ("icon for %s at %dx%d@%dx is %s\n", argv[3], size, size, scale, - icon ? gtk_icon_paintable_get_filename (icon) : ""); - - if (icon) - { - GdkPaintable *paintable = GDK_PAINTABLE (icon); + file ? g_file_get_uri (file) : ""); - g_print ("texture size: %dx%d\n", gdk_paintable_get_intrinsic_width (paintable), gdk_paintable_get_intrinsic_height (paintable)); - - g_object_unref (icon); - } + g_print ("texture size: %dx%d\n", gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (icon)), gdk_paintable_get_intrinsic_height (GDK_PAINTABLE (icon))); + g_object_unref (icon); } else { diff --git a/testsuite/gtk/icontheme.c b/testsuite/gtk/icontheme.c index 6d84adc5d8..1760c3a431 100644 --- a/testsuite/gtk/icontheme.c +++ b/testsuite/gtk/icontheme.c @@ -58,6 +58,8 @@ assert_icon_lookup_size (const char *icon_name, gint pixbuf_size) { GtkIconPaintable *info; + GFile *file; + char *path = NULL; if (fallbacks) { @@ -78,21 +80,30 @@ assert_icon_lookup_size (const char *icon_name, return; } + file = gtk_icon_paintable_get_file (info); + if (file) + { + path = g_file_get_path (file); + g_object_unref (file); + } + if (filename) { - if (!g_str_has_suffix (gtk_icon_paintable_get_filename (info), filename)) + if (path == NULL || !g_str_has_suffix (path, filename)) { g_error ("Icon for \"%s\" with flags %s at size %d should be \"...%s\" but is \"...%s\"", icon_name, lookup_flags_to_string (flags), size, - filename, gtk_icon_paintable_get_filename (info) + strlen (g_get_current_dir ())); + filename, path); return; } } else { - g_assert (gtk_icon_paintable_get_filename (info) == NULL); + g_assert (path == NULL); } + g_free (path); + g_assert_cmpint (gdk_paintable_get_intrinsic_width (GDK_PAINTABLE (info)), ==, size); g_object_unref (info); -- 2.30.2